package com.agent.demo;

import java.lang.instrument.Instrumentation;
import java.lang.instrument.UnmodifiableClassException;
import java.util.Arrays;

public class AgentMain {
    public static void agentmain(String agentArgs, Instrumentation inst) {
        System.out.println("----------agentmain 执行了 agentArgs = " + agentArgs);
        inst.addTransformer(new DefineTransformer(), true);
        Class<?>[] classes = Arrays.stream(inst.getAllLoadedClasses()).filter(clz -> clz.getName().endsWith("TestService")).toArray(Class[]::new);
        for (Class<?> clz : classes) {
            System.out.println("扫描到目标类: " + clz.getName());
        }
        try {
            // 增强目标类
            inst.retransformClasses(classes);
        } catch (UnmodifiableClassException e) {
            e.printStackTrace();
        }
    }
}
